#!/usr/bin/env python
################################################################################
#            __  __            _   ____        _ _ ____        _              #
#           |  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_           #
#           | |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|         #
#           | |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_          #
#           |_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|          #
#                                                                             #
#                                     by                                       #
# _______ .______       _______  _______   ______   ______    _______   _______ #
#|   ____||   _  \     |   ____||   ____| /      | /  __  \  |       \ |   ____| #
#|  |__   |  |_)  |    |  |__   |  |__   |  ,----'|  |  |  | |  .--.  ||  |__     #
#|   __|  |      /     |   __|  |   __|  |  |     |  |  |  | |  |  |  ||   __|     #
#|  |     |  |\  \----.|  |____ |  |____ |  `----.|  `--'  | |  '--'  ||  |____     #
#|__|     | _| `._____||_______||_______| \______| \______/  |_______/ |_______|   #
#                                                                                 #
#                          Channel Management Module                             #
#                             Modular FrameWork                                 #
#
# Coders
#
#	Justin Giorgi (rebel_kid)
#
# Debuggers
#
#	Justin Giorgi (rebel_kid)
#	Lachlan Gilmour (chilli0)
#	David Liedle (spiderbyte)
#	(Polarina)
#	(prologic)
#
################################################################################
#                                                                              #
#                               GPL Licensed                                   #
#                                                                              #
################################################################################

# Imports
import time

# Checks flooders based on channel delay
def chandelay(globalvars, msgvars):

	# Checks if the last request was made by the source
	if msgvars["chanid"] in globalvars["lastnick"].keys() and msgvars["chanid"] in globalvars["delays"].keys():
		if globalvars["lastnick"][msgvars["chanid"]] == msgvars["source"]:
			if time.time() - globalvars["delays"][msgvars["chanid"]] >= msgvars["delay"]:
				approved = True
			else:
				approved = False
		else:
			approved = True
	else:
		approved = True
		
	# Notes request in lastnick
	globalvars["lastnick"][msgvars["chanid"]] = msgvars["source"]

	# Checks if user is identified and flood exempt
	if msgvars["accessperms"][8] and msgvars["nickid"] in globalvars["identified"]:
		approved = True

	# Return
	return approved

def floodprotection(globalvars, msgvars):
	approved = chandelay(globalvars, msgvars)
	return approved
	
def autoload(globalvars):
	
	# Loads globalalways
	globalalways = {'hardcoded-floodprotection':floodprotection}
	for feature in globalalways.items():
		if feature[0] not in globalvars["globalalways"].keys():
			globalvars["globalalways"][feature[0]] = feature[1]
	
def load(globalvars, msgvars):
	
	# Loads globalalways
	globalalways = {'hardcoded-floodprotection':floodprotection}
	for feature in globalalways.items():
		if feature[0] not in globalvars["globalalways"].keys():
			globalvars["globalalways"][feature[0]] = feature[1]
		else:
			msgvars["server"].privmsg(msgvars["destination"], "There is already a function named %s." % (feature[0]))
	return True
			
def unload(globalvars, msgvars, reloadmodule = False):
	
	# Unloads globalalways
	globalalways = {'hardcoded-floodprotection':floodprotection}
	for feature in globalalways.items():
		if feature[0] in globalvars["globalalways"].keys():
			del globalvars["globalalways"][feature[0]]
	return True
